package wiki.generic.concretebar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;



import wiki.generic.bar.BarLink;
import wiki.generic.bar.SideBar;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 *
 */
public class ConcreteSideBar implements SideBar{

	private HashMap<String, List<BarLink>> bar=new HashMap<String, List<BarLink>>();
	private List<String> categories=new ArrayList<String>();
	
	
	public ConcreteSideBar(HashMap<String, List<BarLink>> bar,
			List<String> categories) {
		super();
		this.bar = bar;
		this.categories = categories;
	}


	@Override
	public HashMap<String, List<BarLink>> getMapLinks() {
		return bar;
	}


	@Override
	public List<String> getCategories() {
		return categories;
	}
	
	private void addCategory(String category){
		categories.add(category);
		bar.put(category, new ArrayList<BarLink>());
	}
	
	public void addLink (String category, BarLink link){
		if(bar.containsKey(category)){
			bar.get(category).add(link);
		} else {
			addCategory(category);
			bar.get(category).add(link);
		}
	}
	
	public void deleteCategory(String name){
		for (int i = 0; i < categories.size(); i++) {
			String cat=categories.get(i);
			if (cat.compareTo(name)==0) {
				categories.remove(i);
			}
		}
		bar.remove(name);
	}
	
	private int getLinkPosition(String category, String linkName){
		List<BarLink> links=bar.get(category);
		for (int i = 0; i < links.size(); i++) {
			if(links.get(i).getLinkName().compareTo(linkName)==0){
				return i;
			}
		}
		return -1;
	}


	@Override
	public void deleteLink(String category, String linkName) {
		if(bar.containsKey(category)){
			int i=getLinkPosition(category, linkName);
			if(i>-1){
				bar.get(category).remove(i);
			}
		}
	}


	@Override
	public void renameLink(String category, String linkName, String newName) {
		if(bar.containsKey(category)){
			int i=getLinkPosition(category, linkName);
			if(i>-1){
				bar.get(category).get(i).setLinkName(newName);
			}
		}
		
	}
	
}
